package com.ruoyi.yard.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yard.domain.CompWarehouseInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yard.domain.DeliveryPlanInfo;
import com.ruoyi.yard.service.IDeliveryPlanInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发货计划Controller
 * 
 * @author ruoyi
 * @date 2022-09-05
 */
@RestController
@RequestMapping("/yard/DeliveryPlanInfo")
public class DeliveryPlanInfoController extends BaseController
{
    @Autowired
    private IDeliveryPlanInfoService deliveryPlanInfoService;

    /**
     * 查询发货计划列表
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeliveryPlanInfo deliveryPlanInfo)
    {
        startPage();
        List<DeliveryPlanInfo> list = deliveryPlanInfoService.selectDeliveryPlanInfoList(deliveryPlanInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:list')")
    @GetMapping("/homePage/list")
    public TableDataInfo homePageList(DeliveryPlanInfo deliveryPlanInfo)
    {
        startPage();
        List<DeliveryPlanInfo> list = deliveryPlanInfoService.homePageList(deliveryPlanInfo);
        return getDataTable(list);
    }


    /**
     * 查询发货计划列表-统计
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:statistics')")
    @GetMapping("/statistics")
    public AjaxResult statistics(DeliveryPlanInfo deliveryPlanInfo)
    {

        List<DeliveryPlanInfo> list = deliveryPlanInfoService.homePageList(deliveryPlanInfo);
        Long count=0L;
        Double weight=0D;
        for (DeliveryPlanInfo c:list){
            count=count+c.getPlanCount();
            weight=weight+c.getPlanWeight()*c.getPlanCount();
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("count",count);
        ajaxResult.put("weight",weight);
        return ajaxResult;
    }
    /**
     * 导出发货计划列表
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:export')")
    @Log(title = "发货计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliveryPlanInfo deliveryPlanInfo)
    {
        List<DeliveryPlanInfo> list = deliveryPlanInfoService.homePageList(deliveryPlanInfo);
        ExcelUtil<DeliveryPlanInfo> util = new ExcelUtil<DeliveryPlanInfo>(DeliveryPlanInfo.class);
        util.exportExcel(response, list, "发货计划数据");
    }

    /**
     * 获取发货计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(deliveryPlanInfoService.selectDeliveryPlanInfoById(id));
    }

    /**
     * 新增发货计划
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:add')")
    @Log(title = "发货计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryPlanInfo deliveryPlanInfo)
    {
        if (deliveryPlanInfo.getProjectNo()==null)
            throw new ServiceException("项目名称不能为空!");

        if (deliveryPlanInfo.getNeedCompany()==null)
            throw new ServiceException("要货单位不能为空!");

        if(deliveryPlanInfo.getReceivingAddress()==null)
            throw new ServiceException("收货地址不能为空!");
        if (deliveryPlanInfo.getDeliverGoodsTime()==null)
            throw new ServiceException("发货时间不能为空!");

        return toAjax(deliveryPlanInfoService.insertDeliveryPlanInfo(deliveryPlanInfo));
    }

    /**
     * 修改发货计划
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:edit')")
    @Log(title = "发货计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeliveryPlanInfo deliveryPlanInfo)
    {
        return toAjax(deliveryPlanInfoService.updateDeliveryPlanInfo(deliveryPlanInfo));
    }

    /**
     * 删除发货计划
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:remove')")
    @Log(title = "发货计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(deliveryPlanInfoService.deleteDeliveryPlanInfoById(id));
    }

    /**
     * 审核发货计划
     */
    @PreAuthorize("@ss.hasPermi('yard:DeliveryPlanInfo:toExamine')")
    @Log(title = "发货计划", businessType = BusinessType.INSERT)
    @PostMapping("/toExamine")
    public AjaxResult toExamine(@RequestBody JSONObject params)
    {
        return toAjax(deliveryPlanInfoService.toExamine(params));
    }
}
